package sample.sharding.key;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.shardingsphere.core.strategy.keygen.SnowflakeShardingKeyGenerator;

/**
 * @date 2020/9/1
 */
public class KeySample {

	public static void main(String[] args) throws ParseException {
		SnowflakeShardingKeyGenerator generator = new SnowflakeShardingKeyGenerator();
		generator.getProperties().setProperty("worker.id","1");
		Long id = (Long) generator.generateKey();
		System.out.println(id);
		System.out.println(fixLengthBins(Long.toBinaryString(id)));
		//右移22位, 高23位与0
		//0000 0000 0000 0000 0000 0001 1111 1111 1111 1111 11 11 1111 1111 1111 1111 1111
		System.out.println(fixLengthBins(Long.toBinaryString(id >> 22)));
		System.out.println(fixLengthBins(Long.toBinaryString(id >> 22 & 0x1ff_ffff_ffffL)));
		System.out.println(SnowflakeShardingKeyGenerator.EPOCH + (id >> 22 & 0x1ff_ffff_ffffL));
		System.out.println(new Date(SnowflakeShardingKeyGenerator.EPOCH + (id >> 22 & 0x1ff_ffff_ffffL)));

		Calendar calendar = Calendar.getInstance();
		System.out.println(calendar.get(Calendar.DAY_OF_YEAR));

		System.out.println("step:1".split(":")[0]);
		System.out.println("step".split(":")[0]);

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date = format.parse("20200917");
		SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		System.out.println(format2.format(date));
		long x = 0x3f_ffffL;

		String s = "nacos://ss";
		System.out.println(s.substring(8));
	}

	public static String fixLengthBins(String str) {
		StringBuilder bins = new StringBuilder(str);
		int len = bins.length();
		for(int i=0;i<64-len;i++) {
			bins.insert(0, '0');
		}
		return bins.toString();
	}
}
